package com.example.backblog.controller.front.user;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import com.example.backblog.annotation.UserIdParameter;
import com.example.backblog.dto.user.ResetPasswordDto;
import com.example.backblog.dto.user.UserInfo;
import com.example.backblog.entity.user.User;
import com.example.backblog.repository.user.UserRepository;
import com.example.backblog.util.ConstantUtil;
import com.example.backblog.util.ErrorCodeUtil;
import com.example.backblog.vo.ApiResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Optional;

@RestController
@RequestMapping("/user")
@Log4j2
public class UserController {
    private static final int MAX_SIZE = 1024*1024;
    @Autowired
    private UserRepository userRepository;
    @Value("${file.user-path}")
    private String userPicturePath;
    @Value("${my.config.httpPrefix}")
    private String httpPrefix;
    @PostMapping("/resetPassword")
    public ApiResult resetPassword(@UserIdParameter Long userId, @RequestBody ResetPasswordDto resetPasswordDto) throws Exception {

//        log.info(userId);
        Optional<User> byId = userRepository.findById(userId);
        if (byId.isPresent()) {
            User user = byId.get();
            if (user.getPassword().equals(resetPasswordDto.getOldPassword())) {
                user.setPassword(resetPasswordDto.getNewPassword());
                userRepository.save(user);
                return ApiResult.success();
            }else {
                throw new Exception("密码不正确");
            }
        }else{
            throw new Exception("用户不存在");
        }
    }
    @GetMapping("/status")
    public ApiResult loginStatus(){
        return ApiResult.success();
    }

    @PostMapping("/updatePicture")
    public ApiResult updatePicture(@UserIdParameter Long userId, MultipartFile file) throws Exception {
        String fileName = UUID.fastUUID().toString() +"_"+userId+".jpg";
        String url = httpPrefix+"/"+fileName;
        String path = userPicturePath+ File.separator+ fileName;
        Optional<User> byId = userRepository.findById(userId);
        if (!byId.isPresent()) {
            throw new Exception("用户不存在");
        }
        if (file.getSize()>=MAX_SIZE) {
            throw new Exception("文件太大");
        }
        User user = byId.get();
        user.setUserPicture(url);
        userRepository.save(user);
        user.setUserPicture(path);
        File savePath = new File(path);
        file.transferTo(savePath);
        return ApiResult.success();
    }
    @GetMapping("/userPicture")
    public  ApiResult userPicture(@UserIdParameter Long userId, HttpServletResponse httpServletResponse) throws Exception {
//        Optional<User> byId = userRepository.findById(userId);
//        if (!byId.isPresent()) {
//            throw new Exception("用户不存在");
//        }
//        return
        return null;
    }
    @GetMapping("/userInfo")
    public  ApiResult userInfo(@UserIdParameter Long userId, HttpServletResponse httpServletResponse) throws Exception {
        Optional<User> byId = userRepository.findById(userId);
        if (!byId.isPresent()) {
            throw new Exception("用户不存在");
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(byId.get(),userInfo);
        return ApiResult.success(userInfo);
    }
}
